/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractCommandController;
import model.Usuario;
import model.Viaje;
import model.Participante;
import java.util.List;
import org.springframework.beans.support.PagedListHolder;
import service.UsuarioService;
import service.ViajeService;
import service.ParticipanteService;
import funcion.enviarMail;
import java.util.ArrayList;
import org.springframework.web.servlet.mvc.Controller;
import org.apache.log4j.*;
import org.apache.log4j.xml.DOMConfigurator;

/**
 * @author Fatima Pestana
 * @author Geral Zerpa Mora
 */
public class MisViajesHayTour implements Controller {

    private UsuarioService usuarioService;
    private ViajeService viajeService;
    private ParticipanteService participanteService;
    private static Logger loggers = null;

    public void setViajeService(ViajeService viajeService) {
        this.viajeService = viajeService;
    }

    public void setParticipanteService(ParticipanteService participanteService) {
        this.participanteService = participanteService;
    }

    public void setUsuarioService(UsuarioService usuarioService) {
        this.usuarioService = usuarioService;
    }

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

        Map model = new HashMap();

        PagedListHolder pagedListHolder1 = (PagedListHolder) request.getSession().getAttribute("clienteList");
        PagedListHolder pagedListHolder2 = (PagedListHolder) request.getSession().getAttribute("clienteList2");
        Usuario user2;
        System.out.println("aqui voy de nuevo");
        user2 = (Usuario) usuarioService.getUsuario(Long.parseLong(request.getParameter("ux")));
        Long Valor;
        Valor = Long.parseLong(request.getParameter("AM"));
        System.out.println("valor de otra " + Valor);

        if (Valor == 1) {
            pagedListHolder1 = new PagedListHolder(viajeService.listdeMisViajes(Long.parseLong(request.getParameter("ux"))));
            List misv;
            misv = participanteService.getMisViajesPar(Long.parseLong(request.getParameter("ux")));
            if (misv == null) {
                pagedListHolder2 = new PagedListHolder(misv);
            } else {
                List buscados = new ArrayList();
                for (int i = 0; i < misv.size(); i++) {
                    Viaje v;
                    Participante p;
                    p = (Participante) misv.get(i);
                    if (p.getEstado().equals("INVITADO")){
                        v = (Viaje) viajeService.getViaje(p.getViaje().getIdViaje());
                        buscados.add(v);
                    }
                }
                pagedListHolder2 = new PagedListHolder(buscados);
            }
        } else {
            String page = (String) request.getParameter("page");
            if ("next".equals(page)) {
                pagedListHolder1.nextPage();
            } else if ("previous".equals(page)) {
                pagedListHolder1.previousPage();
            }
        }

        request.getSession().setAttribute("clienteList", pagedListHolder1);
        model.put("clienteList", pagedListHolder1);
        request.getSession().setAttribute("clienteList2", pagedListHolder2);
        model.put("clienteList2", pagedListHolder2);
        model.put("usuario", user2);
        return new ModelAndView("viajesHayTour", model);

    }
}
